package com.sdhs.costcontrol.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.sdhs.util.BidUtilization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.costcontrol.domain.GcControlManagementCostMaintenance;
import com.sdhs.costcontrol.mapper.GcControlManagementCostMaintenanceMapper;
import com.sdhs.costcontrol.service.IGcControlManagementCostMaintenanceService;
import com.sdhs.myproject.domain.GcMyprojectProfitMarginMeasurement;
import com.sdhs.myproject.mapper.GcMyprojectProfitMarginMeasurementMapper;

/**
 * 管理成本维护Service业务层处理
 * 
 * @author wanglei
 * @date 2025-03-12
 */
@Service
public class GcControlManagementCostMaintenanceServiceImpl implements IGcControlManagementCostMaintenanceService 
{
    @Autowired
    private GcControlManagementCostMaintenanceMapper gcControlManagementCostMaintenanceMapper;
    
    @Autowired
    private GcMyprojectProfitMarginMeasurementMapper measurementMapper;
    
    /**
     * 累计费用不能超过管理成本上限
     * @param gcControlManagementCostMaintenance
     * @return
     */
    public boolean checkCostSum(GcControlManagementCostMaintenance costMaintenance) {
    	boolean result = true;
    	GcMyprojectProfitMarginMeasurement measurement = measurementMapper.selectGcMyprojectProfitMarginMeasurementById(costMaintenance.getProjectId());
    	if (measurement != null && measurement.getOverheadCostCap() != null) {
    		BigDecimal costSum = gcControlManagementCostMaintenanceMapper.selectCostSum(costMaintenance);
    		if (costSum == null) {
    			costSum = BigDecimal.ZERO;
    		}
    		BigDecimal cost = costMaintenance.getCost();
    		BigDecimal costTotal = costSum.add(cost).setScale(2, RoundingMode.HALF_UP);
    		if (costTotal.compareTo(measurement.getOverheadCostCap()) > 0) {
    			result = false;
    		}
    	}
    	return result;
    }

    /**
     * 查询管理成本维护
     * 
     * @param id 管理成本维护主键
     * @return 管理成本维护
     */
    @Override
    public GcControlManagementCostMaintenance selectGcControlManagementCostMaintenanceById(Long id)
    {
        return gcControlManagementCostMaintenanceMapper.selectGcControlManagementCostMaintenanceById(id);
    }

    /**
     * 查询管理成本维护列表
     * 
     * @param gcControlManagementCostMaintenance 管理成本维护
     * @return 管理成本维护
     */
    @Override
    public List<GcControlManagementCostMaintenance> selectGcControlManagementCostMaintenanceList(GcControlManagementCostMaintenance gcControlManagementCostMaintenance)
    {
        return gcControlManagementCostMaintenanceMapper.selectGcControlManagementCostMaintenanceList(gcControlManagementCostMaintenance);
    }

    /**
     * 新增管理成本维护
     * 
     * @param gcControlManagementCostMaintenance 管理成本维护
     * @return 结果
     */
    @Override
    public int insertGcControlManagementCostMaintenance(GcControlManagementCostMaintenance gcControlManagementCostMaintenance)
    {
		gcControlManagementCostMaintenance.setCreater(SecurityUtils.getUsername());
        gcControlManagementCostMaintenance.setCreateTime(DateUtils.getNowDate());
        int i = gcControlManagementCostMaintenanceMapper.insertGcControlManagementCostMaintenance(gcControlManagementCostMaintenance);
        /*新增管理成本维护的时候计算我的项目-利润率测试-实施期利润率*/
        BidUtilization.UpdateBidUtilization(gcControlManagementCostMaintenance.getProjectId());
        return i;
    }

    /**
     * 修改管理成本维护
     * 
     * @param gcControlManagementCostMaintenance 管理成本维护
     * @return 结果
     */
    @Override
    public int updateGcControlManagementCostMaintenance(GcControlManagementCostMaintenance gcControlManagementCostMaintenance)
    {
		gcControlManagementCostMaintenance.setUpdater(SecurityUtils.getUsername());
        gcControlManagementCostMaintenance.setUpdateTime(DateUtils.getNowDate());
         int i = gcControlManagementCostMaintenanceMapper.updateGcControlManagementCostMaintenance(gcControlManagementCostMaintenance);
        /*修改管理成本维护的时候计算我的项目-利润率测试-实施期利润率*/
        BidUtilization.UpdateBidUtilization(gcControlManagementCostMaintenance.getProjectId());
        return i;
    }

    /**
     * 批量删除管理成本维护
     * 
     * @param ids 需要删除的管理成本维护主键
     * @return 结果
     */
    @Override
    public int deleteGcControlManagementCostMaintenanceByIds(Long[] ids)
    {
        return gcControlManagementCostMaintenanceMapper.deleteGcControlManagementCostMaintenanceByIds(ids);
    }

    /**
     * 删除管理成本维护信息
     * 
     * @param id 管理成本维护主键
     * @return 结果
     */
    @Override
    public int deleteGcControlManagementCostMaintenanceById(Long id)
    {
        return gcControlManagementCostMaintenanceMapper.deleteGcControlManagementCostMaintenanceById(id);
    }
}
